
let loader = require('../loader');
let ResType = require('./resType');

class ResItem {
    constructor(filepath, type = ResType.Text) {
        this.filepath = filepath;
        this.type = type;
        this.data = null;
    }

    getKey() {
        if (Array.isArray(this.filepath)) {
            return this.filepath[0];
        } else {
            return this.filepath;
        }
    }

    load(cb) {
        switch (this.type) {
            case ResType.Text:
                loader.loadText(this.filepath).then(content => {
                    this.data = content;
                    cb(true, this.getKey());
                });
                break;
            case ResType.Image:
                loader.loadImage(this.filepath).then(content => {
                    this.data = content;
                    cb(true, this.getKey());
                });
                break;
            case ResType.CubeImage:
                loader.loadCubeImage(this.filepath).then(content => {
                    this.data = content;
                    cb(true, this.getKey());
                });
                break;
        }
    }
}

module.exports = ResItem;
